package com.ym.controller.management;

import com.github.pagehelper.PageInfo;
import com.ym.base.enums.FlagEnum;
import com.ym.common.annotations.CustomConstraint;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;


import com.ym.core.utils.tkmybatis.TK;
import com.ym.medical.domain.entity.ClinicManualEntity;
import com.ym.medical.domain.param.ClinicManualParam;
import com.ym.medical.enums.CodeRuleEnum;
import com.ym.medical.service.IClinicManualSV;
import com.ym.medical.service.ISortTableSV;
import com.ym.medical.service.ISysCodeRuleSV;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 订单相关
 *
 * @author: 李涛
 * @version: 2019年06月24日 14:45
 */
@Api(tags = "操作手册")
@RestController
@RequestMapping("/management/manual")
@Validated
public class ClinicManualController extends BaseController {

	@Autowired
	private IClinicManualSV clinicManualSV;

	@Autowired
	private ISortTableSV sortTableSV;

	@Autowired
	private ISysCodeRuleSV generateCodeRuleSV;

	@ApiOperation("操作手册列表")
	@GetMapping(value = "/operationManual/list")
	public APIResponse<PageInfo<ClinicManualEntity>> operationManualList(
			ClinicManualParam query,
			@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int limit
	) {
		query.setDeleted(FlagEnum.NO.getKey());
		PageDomain.startPage(pageNo, limit,"sort","desc");
		List<ClinicManualEntity> clinicManualPageInfo = clinicManualSV.selectList(query.covert());
		return successPage(clinicManualPageInfo);
	}

	@ApiOperation("操作手册新增修改")
	@PostMapping(value = "/operationManual/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse operationManualUpdate(
			@RequestBody ClinicManualParam clinicManual
	) {
		if (clinicManual.getId() != null) {
			//  code不能修改
			clinicManual.setTheCode(null);
			clinicManualSV.updateById(clinicManual.covert());
		} else {
			String code = generateCodeRuleSV.createCode(CodeRuleEnum.ARTICLE_CODING);
			clinicManual.setTheCode(code);
			clinicManual.setCreateTime(new Date());
			clinicManual.setDeleted(FlagEnum.NO.getKey());
			clinicManual.setState(FlagEnum.NO.getKey());
			clinicManualSV.insert(clinicManual.covert());
			//序号设置为ID
			ClinicManualEntity updateSort = new ClinicManualEntity();
			updateSort.setId(clinicManual.getId());
			updateSort.setSort(clinicManual.getId().intValue());
			clinicManualSV.updateById(updateSort);
		}
		return success();
	}

	@ApiOperation("操作手册删除")
	@PostMapping(value = "/operationManual/delete", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse operationManualDelete(
			@RequestBody @NotNull Long[] ids
	) {
		clinicManualSV.deleteByIds(ids);
		return success();
	}

	@ApiOperation("操作手册发布")
	@PostMapping(value = "/operationManual/release", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse operationManualRelease(
			@RequestBody @NotNull Long[] ids,
			@RequestParam @NotEmpty @CustomConstraint(FlagEnum.class) String state
	) {
		for (Long id : ids) {
			ClinicManualEntity clinicManual = new ClinicManualEntity();
			clinicManual.setId(id);
			clinicManual.setState(state);
			clinicManualSV.updateById(clinicManual);
		}
		return success();
	}

	@ApiOperation("操作手册置顶")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "是否置顶，KEY=FlagEnum", name = "top", paramType = "query"),
			@ApiImplicitParam(value = "要置顶的ID", name = "ids", paramType = "body")
	})
	@PostMapping(value = "/operationManual/top", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse operationManualTop(
			@RequestBody @NotNull Long[] ids,
			@RequestParam @NotEmpty @CustomConstraint(FlagEnum.class) String top
	) {
		for (Long id : ids) {
			ClinicManualEntity clinicManual = clinicManualSV.selectById(id);
			String[] params = {clinicManual.getContentType(), clinicManual.getReadRole()};
			List<ClinicManualEntity> first = clinicManualSV.selectList(
					TK.selectWhere(ClinicManualEntity.class)
							.andEqualTo(ClinicManualEntity::getReadRole,clinicManual.getReadRole())
							.andEqualTo(ClinicManualEntity::getContentType,clinicManual.getContentType())
					.end().orderBy(ClinicManualEntity::getSort).desc().limit(1));
			sortTableSV.sortTable("clinic_manual", null, clinicManual.getId(), first.get(0).getId(), params);
		}
		return success();
	}

}
